package com.jianying.day04;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.configuration.MemorySize;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.connector.file.sink.FileSink;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.OutputFileConfig;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.time.Duration;

/**
 * ClassName: Flink08_Sink_File
 * Package: com.atguigu.day04
 * Description:
 *
 * @Author: tubage
 * @Create: 2024/4/2 15:30
 * @Version: 1.0
 */
public class Flink08_Sink_File {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // TODO 如果将六种数据写到文件系统个，必须开检查点 否则一直都是 .inprogress
        env.enableCheckpointing(2000, CheckpointingMode.EXACTLY_ONCE);

        // TODO 生成模拟数据
        DataGeneratorSource<String> source = new DataGeneratorSource<>(
                new GeneratorFunction<Long, String>() {
                    @Override
                    public String map(Long value) throws Exception {
                        return "数据：" + value;
                    }
                },
                Long.MAX_VALUE,
                RateLimiterStrategy.perSecond(100),
                Types.STRING
        );

        DataStreamSource<String> dataGenDS = env.fromSource(source, WatermarkStrategy.noWatermarks(), "dataGen");

        FileSink<String> sink = FileSink
                .forRowFormat(new Path("/Users/china/Atguigu/05_Code/bigdata-flink/output"), new SimpleStringEncoder<String>("UTF-8"))
                .withOutputFileConfig(new OutputFileConfig("atguigu-", ".log"))
                .withRollingPolicy(
                        DefaultRollingPolicy.builder()
                                .withRolloverInterval(Duration.ofMinutes(15)) // 每15分钟滚动一次
                                .withInactivityInterval(Duration.ofMinutes(5)) // 距离上条数据到来时间
                                .withMaxPartSize(MemorySize.ofMebiBytes(1024))
                                .build())
                .build();


        dataGenDS.sinkTo(sink);

        env.execute();
    }
}
